// pages/myChild/myChild.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    birthday: "",
    studentNumber: "",
    schoolName: "",
    className: "",
    height: "",
    weight: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      studentNumber: options.studentNumber
    })

    //根据学籍号获取学生信息
    this.getStuDetail(options.studentNumber);
  },
  toHealth(e) {
    let type = e.currentTarget.dataset.type;
    let height, weight;
    if (type == 1) {
      height = this.data.height == "暂无" ? "" : this.data.height;
      weight = "";
    } else {
      height = "";
      weight = this.data.weight == "暂无" ? "" : this.data.weight;
    }

    wx.navigateTo({
      url: '/pages/mine/editHealthInfo/editHealthInfo?type=' + type + "&height=" + height + "&weight=" + weight,
    })
  },
  //解除绑定
  removeBind() {
    let that = this;
    wx.getStorage({
      key: 'parent',
      success: function(res) {
        let parentId = res.data.stdtPantId;
        app.api.ajaxSubmit("/node/wx/mine/bind/remove", {
          studentNumber: that.data.studentNumber,
          parentId: parentId
        }).then(result => {
          if (result.data.error == 10000) {
            wx.setStorageSync("isBound", 0);
            wx.removeStorageSync("student");
            wx.removeStorageSync("parent");
            wx.removeStorageSync("parents");
            wx.removeStorageSync("height");
            wx.removeStorageSync("weight");
            wx.removeStorageSync("deviceNum");
            wx.reLaunch({
              url: '/pages/login/register/register',
            })
          }
        }).catch(err => {
          wx.showToast({
            title: '服务器开小差',
            icon: "none",
            duration: 2000
          })
        })
      },
    })

  },
  getStuDetail(stuNum) {
    let that = this;
    app.api.ajaxSubmit("/node/wx/mine/student/detail", {
      studentNumber: stuNum
    }).then(result => {
      let info = result.data.data;
      let className = info.className ?
        info.gradeName + info.number + "班（" + info.className + "）" : info.gradeName + info.number + "班";
      that.setData({
        name: info.name,
        birthday: new Date(info.birthday).format("yyyy-MM-dd"),
        studentNumber: info.studentNumber,
        schoolName: info.schoolName,
        className: className,
        height: info.healthInfo && info.healthInfo.height ? info.healthInfo.height : "暂无",
        weight: info.healthInfo && info.healthInfo.weight ? info.healthInfo.weight : "暂无"
      })
    }).catch(err => {
      wx.showToast({
        title: '服务器开小差',
        icon: "none",
        duration: 2000
      })
    })
  }
})